﻿using NewLife.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfBill.Common;
using WpfBill.Data;

namespace WpfBill.Controls
{
    /// <summary>
    /// UCProject.xaml 的交互逻辑
    /// </summary>
    public partial class UCProject : UserControl
    {
        /// <summary>
        /// 单位信息集合
        /// </summary>
        private IList<Enterprise> enterprises = null;
        /// <summary>
        /// 当前单位信息
        /// </summary>
        private Enterprise _Enterprise = null;

        public UCProject()
        {
            InitializeComponent();
            Init();
        }
        private void Init()
        {
            ucPage.PageChanged += UcPage_PageChanged;
            ucPage.PageSizeChanged += UcPage_PageSizeChanged;
            QueryData(0);

            try
            {
                enterprises = Enterprise.FindAll();
                var names = enterprises.Select(p => p.Name).ToArray();
                foreach (var item in names)
                {
                    cmbEnterpriseName.Items.Add(new ComboBoxItem() { Content = item });
                }
                //cmbEnterpriseName.ItemsSource = names;

            }
            catch (Exception ex)
            {

            }
        }
        /// <summary>
        /// 查询单位有关项目
        /// </summary>
        /// <param name="enterprise"></param>
        public void Query(Enterprise enterprise)
        {
            _Enterprise = enterprise;
            QueryData(0);
            try
            {
                cmbEnterpriseName.Items.Clear();
                enterprises = Enterprise.FindAll();
                var names = enterprises.Select(p => p.Name).ToArray();
                foreach (var item in names)
                {
                    cmbEnterpriseName.Items.Add(new ComboBoxItem() { Content = item });
                }
                //cmbEnterpriseName.ItemsSource = names;

            }
            catch (Exception ex)
            {

            }
        }
        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rp"></param>
        private void QueryData(int page)
        {
            try
            {
                int rp = ucPage.PageSize;
                long count = Project.FindCount();
                ucPage.Count = (int)count;
                PageParameter pageParam = new PageParameter() { PageIndex = page, PageSize = rp };

                XCode.Expression where = new XCode.Expression();
                string name = txtName.Text.Trim();
                string name2 = _Enterprise == null ? null : _Enterprise.Name;
                if (!string.IsNullOrEmpty(name))
                    where &= Project._.Name.Contains(name);
                if (!string.IsNullOrEmpty(name2))
                {
                    where &= (Project._.EnterpriseB.Contains(name2) | Project._.EnterpriseA.Contains(name2));

                }
                var list = Project.FindAll(where, pageParam);
                this.dgData.ItemsSource = list;
            }
            catch (Exception ex)
            {

            }
        }
        private void TxtEnterpriseName_TextChanged(object sender, TextChangedEventArgs e)
        {
            QueryData(0);
        }

        private void TxtName_TextChanged(object sender, TextChangedEventArgs e)
        {
            QueryData(0);
        }

        private void CmbEnterpriseName_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {

            QueryData(0);
        }


        private void dgBtnView_Click(object sender, RoutedEventArgs e)
        {
            var project = dgData.SelectedItem as Project;
            if (project != null)
            {
                AppHelper.Main.ShowBill(project);
            }
        }

        private void BtnAdd_Click(object sender, RoutedEventArgs e)
        {
            GUI.WinProject win = new GUI.WinProject();
            win.ShowDialog();
            QueryData(0);
        }
        private void UcPage_PageChanged(object sender, int page, int rp)
        {
            QueryData(page);
        }

        private void UcPage_PageSizeChanged(object sender, int page, int rp)
        {
            QueryData(page);
        }

        private void CmbEnterpriseName_SearchTextChanged(object sender, Panuon.UI.Silver.Core.SearchTextChangedEventArgs e)
        {
            try
            {
                foreach (ComboBoxItem item in cmbEnterpriseName.Items)
                {
                    item.Visibility = item.Content.ToString().Contains(e.Text) ? Visibility.Visible : Visibility.Collapsed;
                }

            }
            catch (Exception ex)
            {

            }
        }

        private void DgData_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            try
            {
                var proInfo = e.Row.Item as Project;
                int index = e.Column.DisplayIndex;
                string header = ((System.Windows.Data.Binding)((System.Windows.Controls.DataGridBoundColumn)e.Column).Binding).Path.Path;
                string value = (e.Column.GetCellContent(e.Row) as TextBox).Text;
                if (proInfo[header].ToString() != value)
                {
                    proInfo[header] = value;
                    proInfo.Save();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }
    }
}
